package com.wudi.estate.service.impl.parkingmanagement;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wudi.estate.bean.communitymanagement.Community;
import com.wudi.estate.bean.ownermanagement.Personnel;
import com.wudi.estate.bean.ownermanagement.Vehicle;
import com.wudi.estate.bean.parkingmanagement.Parking;
import com.wudi.estate.bean.parkingmanagement.ParkingUsage;
import com.wudi.estate.mapper.communitymanagement.CommunityMapper;
import com.wudi.estate.mapper.ownermanagement.PersonnelMapper;
import com.wudi.estate.mapper.ownermanagement.VehicleMapper;
import com.wudi.estate.mapper.parkingmanagement.ParkingMapper;
import com.wudi.estate.mapper.parkingmanagement.ParkingUsageMapper;
import com.wudi.estate.service.parkingmanagement.ParkingUsageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.SessionAttributes;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ParkingUsageServiceImpl implements ParkingUsageService {
    @Autowired
    private ParkingUsageMapper parkingUsageMapper;
    @Autowired
    private ParkingMapper parkingMapper;
    @Autowired
    private PersonnelMapper personnelMapper;
    @Autowired
    private VehicleMapper vehicleMapper;
    @Autowired
    private CommunityMapper communityMapper;
//    查询所有停车位使用和小区，人员，车辆（用于显示列表）
    @Override
    public List<Map<String, Object>> selectAll() throws Exception {
        List<Map<String,Object>> lists = new ArrayList<>();
        List<ParkingUsage> parkingUsages = parkingUsageMapper.selectList(null);
        for (ParkingUsage parkingUsage : parkingUsages) {
            Map<String,Object> maps = new HashMap<>();
//            查找所有的停车位管理信息
            ParkingUsage parkingUsageById = parkingUsageMapper.selectById(parkingUsage.getId());
            Parking parkingById = parkingMapper.selectById(parkingUsageById.getParkingId());
            Community communityById = communityMapper.selectById(parkingById.getCid());
            Personnel personnelById = personnelMapper.selectById(parkingUsageById.getPid());
            Vehicle vehicle = new Vehicle();
            vehicle.setId(parkingUsageById.getVid());
            Vehicle vehicleById = vehicleMapper.selectById(vehicle);
            maps.put("parkingUsageById",parkingUsageById);
            maps.put("parkingById",parkingById);
            maps.put("communityById",communityById);
            maps.put("personnelById",personnelById);
            maps.put("vehicleById",vehicleById);
            lists.add(maps);
        }
        return lists;
    }
//查询车位管理的车位和所属人
    @Override
    public Map<String, Object> selectParkingAndPersonnel() throws Exception {
        Map<String,Object> maps = new HashMap<>();
        QueryWrapper<Parking> parkingQueryWrapper = new QueryWrapper();
//        QueryWrapper<> wrapper = new QueryWrapper();
        parkingQueryWrapper.eq("state",false);
        List<Parking> parkings = parkingMapper.selectList(parkingQueryWrapper);
        List<Personnel> personnels = new ArrayList<>();
        List<Vehicle> vehicles = vehicleMapper.selectAll();
        for (Vehicle vehicle : vehicles) {
            Personnel personnel = personnelMapper.selectById(vehicle.getPid());
            personnels.add(personnel);
        }
        maps.put("parkings",parkings);
        maps.put("personnels",personnels);
        maps.put("vehicles",vehicles);
        return maps;
    }
//添加的方法（用于添加）
    @Override
    public void insert(ParkingUsage parkingUsage) throws Exception {
        Vehicle vehicle = vehicleMapper.selectByPid(parkingUsage.getPid());
        parkingUsage.setVid(vehicle.getId());
        Parking parking= parkingMapper.selectById(parkingUsage.getParkingId());
        parking.setState(true);
        parkingMapper.updateById(parking);
        parkingUsageMapper.insert(parkingUsage);
    }

    @Override
    public ParkingUsage selectParkingUsage(long id) {
        ParkingUsage parkingUsageById = parkingUsageMapper.selectById(id);
        return  parkingUsageById;
    }
//更改信息
    @Override
    public void update(ParkingUsage parkingUsage) throws Exception {
        Vehicle vehicle = new Vehicle();
        Vehicle vehicle1 = vehicleMapper.selectByPid(parkingUsage.getPid());
        parkingUsage.setVid(vehicle1.getId());
        //        更改车位
        ParkingUsage newParkingUsage =parkingUsageMapper.selectById(parkingUsage.getId());
        if(!(newParkingUsage.getParkingId()==parkingUsage.getParkingId())){
             Parking parking= parkingMapper.selectById(parkingUsage.getParkingId());
             parking.setState(!parking.isState());
            Parking newParking= parkingMapper.selectById(newParkingUsage.getParkingId());
            newParking.setState(!parking.isState());
            parkingMapper.updateById(parking);
            parkingMapper.updateById(newParking);
        }
        parkingUsageMapper.updateById(parkingUsage);
    }
//    根据id去查信息
    @Override
    public ParkingUsage selectById(long id) throws Exception {
        ParkingUsage parkingUsage = parkingUsageMapper.selectById(id);
        return parkingUsage;
    }
//删除为更改
    @Override
    public void deleteById(long id) throws Exception {
        ParkingUsage parkingUsage = parkingUsageMapper.selectById(id);
        parkingUsage.setState(false);
        Parking parking= parkingMapper.selectById(parkingUsage.getParkingId());
        parking.setState(false);
        parkingMapper.updateById(parking);
        parkingUsageMapper.updateById(parkingUsage);
    }
}
